package com.shanyi.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Arrays;

/**
 * @author shanyi
 * @create 2024-11-23-17:05
 */
public class MsgUtil {

    public static String feishuMsg() {

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
        ObjectNode zhCnNode = JsonNodeFactory.instance.objectNode();

        // 创建内容数组
        ArrayNode contentArray = JsonNodeFactory.instance.arrayNode();

        // 添加第一个内容数组元素（包含多个标签）
        ArrayNode firstContentElement = JsonNodeFactory.instance.arrayNode();
        addTagElement(firstContentElement, "text", "第一行:", Arrays.asList("bold", "underline"));
        contentArray.add(firstContentElement);

        // 注意：这里的最后一个元素（包含MD文本的复杂结构）需要特别处理
        ArrayNode mdContentElement = JsonNodeFactory.instance.arrayNode();
        ObjectNode mdNode = JsonNodeFactory.instance.objectNode();
        mdNode.put("tag", "md");
        mdNode.put("text", "**mention user**:<at user_id=\"ou_xxxxxx\">Tom</at>\\n**href**:[Open Platform](https://open.feishu.cn)\\n**code block**:\\n```GO\\nfunc main() int64 {\\n    return 0\\n}\\n```\\n**text styles**: **bold**, *italic*, ***bold and italic***, ~underline~,~~lineThrough~~\\n> quote content\\n\\n1. item1\\n    1. item1.1\\n    2. item2.2\\n2. item2\\n --- \\n- item1\\n    - item1.1\\n    - item2.2\\n- item2");
        mdContentElement.add(mdNode);
        contentArray.add(mdContentElement);

        // 将内容数组添加到zh_cn节点
        zhCnNode.set("content", contentArray);
        zhCnNode.put("title", "我是一个标题");

        // 将zh_cn节点添加到根节点
        rootNode.set("zh_cn", zhCnNode);

        // 将根节点转换为JSON字符串
        String result;
        try {
            result = mapper.writeValueAsString(rootNode);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    private static void addTagElement(ArrayNode arrayNode, String tag, String text, Iterable<String> style, String... additionalFields) {
        ObjectNode tagNode = JsonNodeFactory.instance.objectNode();
        tagNode.put("tag", tag);
        if (text != null) {
            tagNode.put("text", text);
        }
        if (style != null) {
            tagNode.putPOJO("style", style);
        }
        for (int i = 0; i < additionalFields.length; i += 2) {
            if (i + 1 < additionalFields.length) {
                tagNode.put(additionalFields[i], additionalFields[i + 1]);
            }
        }
        arrayNode.add(tagNode);
    }

    private static ArrayNode createSingleTagElement(String tag, String text, Iterable<String> style) {
        return createSingleTagElement(tag, text, style, null);
    }

    private static ArrayNode createSingleTagElement(String tag, String text, Iterable<String> style, String... additionalFields) {
        ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
        addTagElement(arrayNode, tag, text, style, additionalFields);
        return arrayNode;
    }
}
